function GetHttpRequest() {
    if (window.XMLHttpRequest) // Gecko
        return new XMLHttpRequest();
    else if (window.ActiveXObject) // IE
        return new ActiveXObject("MsXml2.XmlHttp");
}

function includeJS(sId, fileUrl, source) {
    if ((source != null) && (!document.getElementById(sId))) {
        var oHead = document.getElementsByTagName('HEAD').item(0);
        var oScript = document.createElement("script");
        oScript.type = "text/javascript";
        oScript.id = sId;
        oScript.text = source;

        oHead.appendChild(oScript);
    }
}

function ajaxPageJs(sId, url) {
    var oXmlHttp = GetHttpRequest();
    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            includeJS(sId, url, oXmlHttp.responseText);
        }
    }
    oXmlHttp.open('GET', url, false);//同步操作
    oXmlHttp.send(null);
}

function includeLink(sId, fileUrl, type, rel) {
    if (type == null || undefined) type = "text/css";
    if (rel == null || undefined) rel = "stylesheet";
    var oHead = document.getElementsByTagName('HEAD').item(0);
    var oLink = document.createElement("link");
    oLink.type = type;
    oLink.rel = rel;
    oLink.id = sId;
    oLink.href = fileUrl;
    oHead.appendChild(oLink);
}

function ajaxPageCss(sId, url) {
    var oXmlHttp = GetHttpRequest();
    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            includeLink(sId, url);
        }
    }
    oXmlHttp.open('GET', url, false);//同步操作
    oXmlHttp.send(null);
}

function ajaxPageIcon(sId, url) {
    var oXmlHttp = GetHttpRequest();
    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            includeLink(sId, url, "image/x-icon", "icon");
        }
    }
    oXmlHttp.open('GET', url, false);//同步操作
    oXmlHttp.send(null);
}

ajaxPageIcon("Icon", "/res/images/ico.png");
ajaxPageCss("layuiCss", "/res/layui/css/layui.css");
ajaxPageCss("layerCss", "/res/layui/css/modules/layer/default/layer.css");

ajaxPageJs("jq", "/res/js/jquery-3.4.1.js");

jQuery.fn.serializeJSON = function () {
    var o = {};
    var a = this.serializeArray();
    jQuery.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return JSON.stringify(o);
}

jQuery.each(["getB", "postB", "putB", "deleteB"], function (i, method) {
    jQuery[method] = function (url, data, callback, type, contentType) {
        method = (method == "getB" || method == "get") ? "get" : (method == "postB" || method == "post") ? "post" :
            (method == "putB" || method == "put") ? "put" : (method == "deleteB" || method == "delete") ? "delete" : "get";

        if (jQuery.isFunction(data)) {
            type = type || callback;
            callback = data;
            data = undefined;
        }

        if (contentType == undefined || contentType == null || contentType == "") {
            return jQuery.ajax(jQuery.extend({
                url: url,
                type: method,
                dataType: type,
                data: data,
                success: callback
            }, jQuery.isPlainObject(url) && url));
        } else {
            return jQuery.ajax(jQuery.extend({
                url: url,
                type: method,
                dataType: type,
                data: data,
                contentType: contentType,
                success: callback
            }, jQuery.isPlainObject(url) && url));
        }
    };
});

jQuery.ajaxGET = function (url, data, callback) {
    return jQuery.getB(url, data, callback);
}
jQuery.ajaxPOST = function (url, data, callback) {
    return jQuery.postB(url, data, callback);
}
jQuery.ajaxPUT = function (url, data, callback) {
    return jQuery.putB(url, data, callback);
}
jQuery.ajaxDEL = function (url, data, callback) {
    return jQuery.deleteB(url, data, callback);
}


jQuery.getJsonGET = function (url, data, callback) {
    return jQuery.getB(url, data, callback, "json");
}
jQuery.getJsonPOST = function (url, data, callback) {
    return jQuery.postB(url, data, callback, "json");
}
jQuery.getJsonPUT = function (url, data, callback) {
    return jQuery.putB(url, data, callback, "json");
}
jQuery.getJsonDEL = function (url, data, callback) {
    return jQuery.deleteB(url, data, callback, "json");
}


jQuery.ajaxJsonGET = function (url, data, callback) {
    return jQuery.getB(url, data, callback, "json", "application/json");
}
jQuery.ajaxJsonPOST = function (url, data, callback) {
    return jQuery.postB(url, data, callback, "json", "application/json");
}
jQuery.ajaxJsonPUT = function (url, data, callback) {
    return jQuery.putB(url, data, callback, "json", "application/json");
}
jQuery.ajaxJsonDEL = function (url, data, callback) {
    return jQuery.deleteB(url, data, callback, "json", "application/json");
}


jQuery.showModal = function showModal(msg) {
    let count = $("#myModal").length;
    if (count == 0) {
        let html = "<div class=\"modal fade bs-example-modal-sm\" id=\"myModal\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"myModalLabel\"" +
            "     aria-hidden=\"true\">" +
            "    <div class=\"modal-dialog modal-sm\">" +
            "        <div class=\"modal-content\">" +
            "            <div class=\"modal-header\">" +
            "                <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">×</button>" +
            "                <h4 class=\"modal-title\" id=\"myModalLabel\">系统提示</h4>" +
            "            </div>" +
            "            <div class=\"modal-body\">" + msg + "</div>" +
            "        </div>" +
            "    </div>" +
            "</div>";
        $("body").append(html);
    }
    $('#myModal').modal('show');
}

function getCookie(name) {
    var strcookie = document.cookie;//获取cookie字符串
    var arrcookie = strcookie.split("; ");//分割
    //遍历匹配
    for (var i = 0; i < arrcookie.length; i++) {
        var arr = arrcookie[i].split("=");
        if (arr[0] == name) {
            return arr[1];
        }
    }
    return "";
}